home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tex / rscsencode.shar / rscsencode.c < prev   
Encoding:
C/C++ Source or Header  |  1988-04-18  |  1.5 KB  |  65 lines

  1. /*
  2.  * Stream filter to change 8 bit bytes into printable ASCII which will
  3.  * survive most networks -- especially the RSCS community which seems
  4.  * to gobble up all sorts of the "ordinary" characters.
  5.  * Encoding is into a 64 character set "[A-Z][a-z][0-9]+-". This means
  6.  * that using 4 characters we can safely represent 3 bytes. Some overhead
  7.  * but I can take it.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include "coder.h"
  12. #define MAXPERLINE 79        /* max chars/line */
  13. char *myname;
  14.  
  15. main(argc,argv) 
  16.     char **argv;
  17. {
  18.     register FILE *fin = stdin, *fout = stdout; /* faster in a register */
  19.     register int c, bcount, ccount = MAXPERLINE-1;
  20.     register long word;
  21.  
  22.     myname = argv[0];
  23.     if (argc == 2 && (fin = fopen(argv[1], "r")) == NULL) {
  24.         fprintf(stderr, "%s: ", myname);
  25.         perror(argv[1]);
  26.         exit(1);
  27.     }
  28.     else if (argc > 2) {
  29.         fprintf(stderr, "usage: %s [file]\n", myname);
  30.         exit(1);
  31.     }
  32.  
  33. #define charout(c) \
  34.     putc(c, fout); \
  35.     if (--ccount == 0) { \
  36.         putc('\n', fout); \
  37.         ccount = MAXPERLINE-1; \
  38.     }
  39.  
  40.     fputs(header, fout);
  41.     word = 0;
  42.     bcount = 3;
  43.     while ((c = getc(fin)) != EOF) {
  44.         word <<= 8;
  45.         word |= c;
  46.         if (--bcount == 0) {
  47.             charout(ENCODE((word >> 18) & 077));
  48.             charout(ENCODE((word >> 12) & 077));
  49.             charout(ENCODE((word >>  6) & 077));
  50.             charout(ENCODE((word      ) & 077));
  51.             word = 0;
  52.             bcount = 3;
  53.         }
  54.     }
  55.     /*
  56.      * A trailing / marks end of data.
  57.      * The last partial encoded word follows in hex,
  58.      * preceded by the byte count.
  59.      */
  60.     if (ccount != MAXPERLINE-1)    /* avoid empty lines */
  61.         putc('\n', fout);
  62.     fprintf(fout, "/%d%x\n", 3-bcount, word);
  63.     exit(0);
  64. }
  65.